<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>three.js</title>
  <script src="./js/three.min.js"></script>
  <script src="./js/OrbitControls.js"></script>
 <style>*{margin: 0; padding: 0;}</style>
</head>
<body>
  <script>
    // 创建场景
const scene = new THREE.Scene();

// 创建透视摄像头
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

// 创建 WebGL 渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建点光源
const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(10, 10, 10);
scene.add(pointLight);
var light = new THREE.AmbientLight(0x555555,1.5);
scene.add(light)

// 创建立方体
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
 

// 更换立方体材质
const cubeMaterial = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
cube.material = cubeMaterial;

// 设置摄像头位置
camera.position.z = 5;
let num = 1;
function animate() {
  num+=0.05;
  requestAnimationFrame(animate);

  // 旋转立方体
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  cube.position.y=Math.sin(num);
  cube.position.x=Math.cos(num*0.5);

  // 渲染场景
  renderer.render(scene, camera);
}

animate();

// 创建轨道控制
const controls = new THREE.OrbitControls(camera, renderer.domElement);

// 窗口大小调整
window.addEventListener('resize', onWindowResize, false);

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}
  </script>
</body>
</html>